package com.wxy.service.serviceimpl;

import com.wxy.dao.BookDao;
import com.wxy.dao.UserBookDao;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * @author 47868
 */
@Service
public class RabbitMqServiceImpl {
    private final BookDao bookDao;
    private final UserBookDao userBookDao;
    @Autowired
    public RabbitMqServiceImpl(BookDao bookDao,
                               UserBookDao userBookDao) {
        this.bookDao = bookDao;
        this.userBookDao = userBookDao;
    }
    @Transactional(rollbackFor = Exception.class)
    @RabbitListener(queues = "borrow.queue")
    public void borrowQueue(Map<String,Object> params){
        int bookId=(int) params.get("bookId");
        Long userId=(Long) params.get("userId");
        userBookDao.deleteOneNumByUid(userId);
        bookDao.deleteBookNum(bookId);
    }

    @Transactional(rollbackFor = Exception.class)
    @RabbitListener(queues = "return.queue")
    public void returnQueue(Map<String,Object> params){
        int bookId=(int) params.get("bookId");
        Long userId=(Long) params.get("userId");
        userBookDao.plusOneNumsByUid(userId);
        bookDao.plusBookNum(bookId);
    }
}
